
//补零函数
function zero(n) {
    return n >= 10 ? n : '0' + n
}

/**
 * 获取搜索条件
 */
function getConditions() {
	return conditions;
	console.log('getConditions---' + selector_region.getValue());
	var cond = {
		region:$.parseJSON(JSON.stringify(selector_region.getValue())),
		org:$.parseJSON(JSON.stringify(selector_operateOrg.getValue())),
		company:$.parseJSON(JSON.stringify(selector_operateCompany.getValue())),
		user:$.parseJSON(JSON.stringify(selector_operateUser.getValue())),
		shop:$.parseJSON(JSON.stringify(selector_shop.getValue()))
	}
	
	console.log(cond);
	return $.parseJSON(JSON.stringify(cond));
}

/**
 * 清空店铺搜索框
 */
function clearFormShop() {
	loadSelectorShop();
}

/**
 * 加载店铺搜索条件
 */
function loadSelectorShop() {
	xmSelect.render({
		el: '#id_form_shop', 
		tips: '店铺',
		paging: true,
		pageSize: 100,
		filterable: true,
		toolbar: {
			show: true,
			showIcon: false, // 隐藏图标
			list: [ 'ALL', 'CLEAR']
		},
		remoteSearch: true,
		remoteMethod: function(val, cb, show){
			var conditions = getConditions();
			conditions.name = val;
	        $.post('/web/bi/listShop', conditions, function(res){
	        	cb(res.data);
	        });
		},
		on: function(data){
			var arr = [];
			for (var i = 0; i < data.arr.length; i++) {
				var single = data.arr[i];
				arr.push({name: single.name, value: single.value});
			}
			conditions.shop = JSON.stringify(arr);
			console.log(conditions);
		}
	})
}
var loadIngHtml = '<div style="width: 100%;padding-top:10px;padding-bottom:15px;text-align: center;border: none;">' +
	'<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop" style="font-size: 40px;color: #288892;"></i>' +
	'</div>';
var loadIngHtmlTable = '<div style="width: 100%;padding-top:70px;padding-bottom:70px;text-align: center;border: none;">' +
	'<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop" style="font-size: 40px;color: #288892;"></i>' +
	'</div>';
// 加载店铺销售
//timeType查询的时间类型，0昨日，1近30天
function loadDPXX(timeType,obj) {
	var conditions = getConditions();
	conditions['timeType'] = timeType;
	console.log(conditions);
	$('#id_dpxx_table').html(loadIngHtml);
    $.post('/web/bi/dpxx', conditions, function(res){
    	var h = `<tr height="25" class="font-before">
        			<td>销售金额</td>
        			<td>销售数量</td>
        			<td>平均售价</td>
        			<td>销售单量</td>
        			<td>弃退单量</td>
        		</tr>
        		<tr height="25" class="font-yesterday">
        			<td>` + res.data.yesterday.price + `</td>
        			<td>` + res.data.yesterday.numProduct + `</td>
        			<td>` + res.data.yesterday.priceAvg + `</td>
        			<td>` + res.data.yesterday.numOrder + `</td>
        			<td>` + res.data.yesterday.numOrderBack + `</td>
        		</tr>
        		<tr height="25" class="font-before">
        			<td>` + res.data.before.price + `(前)` + (res.data.addPercent.price.indexOf('-') == -1 ? `<span class="font-color-green">` + res.data.addPercent.price + `</span>` : `<span class="font-color-red">` + res.data.addPercent.price + `</span>`) + `</td>
        			<td>` + res.data.before.numProduct + `(前)` + (res.data.addPercent.numProduct.indexOf('-') == -1 ? `<span class="font-color-green">` + res.data.addPercent.numProduct + `</span>` : `<span class="font-color-red">` + res.data.addPercent.numProduct + `</span>`) + `</td>
        			<td>` + res.data.before.priceAvg + `(前)` + (res.data.addPercent.priceAvg.indexOf('-') == -1 ? `<span class="font-color-green">` + res.data.addPercent.priceAvg + `</span>` : `<span class="font-color-red">` + res.data.addPercent.priceAvg + `</span>`) + `</td>
        			<td>` + res.data.before.numOrder + `(前)` + (res.data.addPercent.numOrder.indexOf('-') == -1 ? `<span class="font-color-green">` + res.data.addPercent.numOrder + `</span>` : `<span class="font-color-red">` + res.data.addPercent.numOrder + `</span>`) + `</td>
        			<td>` + res.data.before.numOrderBack + `(前)` + (res.data.addPercent.numOrderBack.indexOf('-') != -1 ? `<span class="font-color-green">` + res.data.addPercent.numOrderBack + `</span>` : `<span class="font-color-red">` + res.data.addPercent.numOrderBack + `</span>`) + `</td>
        		</tr>`;
    	$('#id_dpxx_table').html(h);
    	$('#id_dpxx_latestupdatetime').html('更新截止：' + res.data.finishTime);
		if(obj) {
			$(obj).siblings().removeClass('btn-selected')
			$(obj).addClass('btn-selected');
		}
    });

}

//加载店铺广告
//timeType查询的时间类型，0昨日，1近30天
function loadDPGG(timeType,obj) {
	var conditions = getConditions();
	conditions['timeType'] = timeType;
	console.log(conditions);
	$('#id_dpgg_table').html(loadIngHtml);
	$.post('/web/bi/dpgg', conditions, function(res){
 	var h = `<tr height="25" class="font-before">
    			<td>广告花费</td>
    			<td>广告销售金额</td>
    			<td>广告销售数量</td>
    			<td>CPC</td>
    			<td>ACOS</td>
    		</tr>
    		<tr height="25" class="font-yesterday">
    			<td>` + res.data.yesterday.spend + `</td>
    			<td>` + res.data.yesterday.sales1d + `</td>
    			<td>` + res.data.yesterday.unitsSoldClicks1d + `</td>
    			<td>` + res.data.yesterday.cpc + `</td>
    			<td>` + res.data.yesterday.acos + `</td>
    		</tr>
    		<tr height="25" class="font-before">
    			<td>` + res.data.before.spend + `(前)` + (res.data.addPercent.spend.indexOf('-') != -1 ? `<span class="font-color-green">` + res.data.addPercent.spend + `</span>` : `<span class="font-color-red">` + res.data.addPercent.spend + `</span>`) + `</td>
    			<td>` + res.data.before.sales1d + `(前)` + (res.data.addPercent.sales1d.indexOf('-') == -1 ? `<span class="font-color-green">` + res.data.addPercent.sales1d + `</span>` : `<span class="font-color-red">` + res.data.addPercent.sales1d + `</span>`) + `</td>
    			<td>` + res.data.before.unitsSoldClicks1d + `(前)` + (res.data.addPercent.unitsSoldClicks1d.indexOf('-') == -1 ? `<span class="font-color-green">` + res.data.addPercent.unitsSoldClicks1d + `</span>` : `<span class="font-color-red">` + res.data.addPercent.unitsSoldClicks1d + `</span>`) + `</td>
    			<td>` + res.data.before.cpc + `(前)` + (res.data.addPercent.cpc.indexOf('-') != -1 ? `<span class="font-color-green">` + res.data.addPercent.cpc + `</span>` : `<span class="font-color-red">` + res.data.addPercent.cpc + `</span>`) + `</td>
    			<td>` + res.data.before.acos + `(前)` + (res.data.addPercent.acos.indexOf('-') != -1 ? `<span class="font-color-green">` + res.data.addPercent.acos + `</span>` : `<span class="font-color-red">` + res.data.addPercent.acos + `</span>`) + `</td>
    		</tr>`;
		$('#id_dpgg_table').html(h);
		$('#id_dpgg_latestupdatetime').html('更新截止：' + res.data.finishTime);
		if(obj) {
			$(obj).siblings().removeClass('btn-selected')
			$(obj).addClass('btn-selected');
		}
 });
}

/**
 * 加载产品TOP10
 * @param tableId
 * @param type 类型，1普通2精品
 * @param timeType 类型，1昨日2近七天3本月4上月
 */
function loadTopProduct(tableId, type, timeType, obj) {
	var conditions = getConditions();
	conditions['type'] = type;
	conditions['timeType'] = timeType;
	$('#' + tableId).html(loadIngHtmlTable);
	$('#' + tableId).attr('border','0');
	$.post('/web/bi/topProduct', conditions, function(res){
		console.log('res---', res);
		var h = `<tr height="25">
    			<th width="8%;">排名</th>
    			<th width="18%;">商品编码</th>
    			<th width="25%;">商品名称</th>
    			<th>店铺</th>
    			<th>FNSKU</th>
    			<th>销售数量</th>
    			<th>销售金额</th>
    		</tr>`;
		for (var i = 0; i < res.data.rows.length; i++) {
			var row = res.data.rows[i];
			h += `<tr height="25" class="font-before">
    			<td>` + row.rank + `</td>
    			<td class="t-left">` + row.productCode + `</td>
    			<td class="t-left">` + row.productName + `</td>
    			<td class="t-right">` + row.shopName + `</td>
    			<td>` + row.fnsku + `</td>
    			<td>` + row.numSale + `</td>
    			<td class="t-right">` + row.amountSale + `</td>
    		</tr>`;
		}
		$('#' + tableId).attr('border','1');
		$('#' + tableId).html(h);

		if(obj) {
			$(obj).siblings().removeClass('btn-selected')
			$(obj).addClass('btn-selected');
		}
	});
}



/**
 * 加载店铺TOP10
 * @param timeType 类型，1昨日2近七天3本月4上月
 */
function loadTopShop(timeType, obj) {
	var conditions = getConditions();
	conditions['timeType'] = timeType;
	$('#id_top_shop_table').html(loadIngHtmlTable);
	$('#id_top_shop_table').attr('border','0');
	$.post('/web/bi/topShop', conditions, function(res){
		var h = `<tr height="25">
				<th width="8%;">排名</th>
				<th width="21%;">店铺</th>
				<th width="21%;">运营人</th>
				<th width="21%;">销售数量</th>
				<th width="21%;">销售金额</th>
				<th width="8%;">公司排名</th>
			</tr>`;
		for (var i = 0; i < res.data.rows.length; i++) {
			var row = res.data.rows[i];
			h += `<tr height="25" class="font-before">
  			<td>` + row.rank + `</td>
  			<td>` + row.shopName + `</td>
  			<td>` + row.operateUserName + `</td>
  			<td>` + row.numSale + `</td>
  			<td>` + row.amountSale + `</td>
  			<td>` + row.allRank + `</td>
  		</tr>`;
		}
		$('#id_top_shop_table').attr('border','1');
		$('#id_top_shop_table').html(h);

		if(obj) {
			$(obj).siblings().removeClass('btn-selected')
			$(obj).addClass('btn-selected');
		}
	});
}



/**
 * 加载精品广告TOP10
 * @param timeType 类型，1昨日2近七天3本月4上月
 * sortType排序类型，0按销售数量降序，1按广告金额降序
 */
function loadTopProductAdvert(timeType, obj, sortType) {
	var conditions = getConditions();
	conditions['timeType'] = timeType;
	conditions['sortType'] = sortType;
	if(sortType==0){
		$('#id_top_product_advert_table').html(loadIngHtmlTable);
		$('#id_top_product_advert_table').attr('border','0');
	}else {
		$('#id_top_product_advert_table_by_advert_desc').html(loadIngHtmlTable);
		$('#id_top_product_advert_table_by_advert_desc').attr('border','0');
	}

	$.post('/web/bi/topProductAdvert', conditions, function(res){
		if(sortType==0){
			var h = `<tr height="25">
				<th width="8%;">排名</th>
    			<th width="13%;">商品编码</th>
    			<th width="20%;">商品名称</th>
    			<th>销售数量</th>
    			<th>广告数量</th>
    			<th>广告占比</th>
    			<th>销售金额</th>
    			<th>广告金额</th>
    			<th>CPC</th>
    			<th>TACOS</th>
			</tr>`;
			for (var i = 0; i < res.data.rows.length; i++) {
				var row = res.data.rows[i];
				h += `<tr height="25" class="font-before">
	  			<td>` + row.rank + `</td>
	  			<td class="t-left">` + row.productCode + `</td>
	  			<td class="t-left">` + row.productName + `</td>
	  			<td>` + row.numSale + `</td>
	  			<td>` + row.numAdvert + `</td>
	  			<td class="t-right">` + row.numSaleAdvertProportion + `</td>
	  			<td>` + row.amountSale + `</td>
	  			<td class="t-right">` + row.amountAdvert + `</td>
	  			<td class="t-right">` + row.cpc + `</td>
	  			<td class="t-right">` + row.acos + `</td>
	  		</tr>`;
			}
			$('#id_top_product_advert_table').attr('border','1');
			$('#id_top_product_advert_table').html(h);
		}else if(sortType==1){
			var h = `<tr height="25">
				<th width="8%;">排名</th>
    			<th width="13%;">商品编码</th>
    			<th width="20%;">商品名称</th>
    			<th>广告金额</th>
    			<th>广告数量</th>
    			<th>销售数量</th>
    			<th>广告占比</th>
    			<th>销售金额</th>
    			<th>CPC</th>
    			<th>TACOS</th>
			</tr>`;
			for (var i = 0; i < res.data.rows.length; i++) {
				var row = res.data.rows[i];
				h += `<tr height="25" class="font-before">
	  			<td>` + row.rank + `</td>
	  			<td class="t-left">` + row.productCode + `</td>
	  			<td class="t-left">` + row.productName + `</td>
	  			<td class="t-right">` + row.amountAdvert + `</td>
	  			<td>` + row.numAdvert + `</td>
	  			<td>` + row.numSale + `</td>
	  			<td class="t-right">` + row.numSaleAdvertProportion + `</td>
	  			<td>` + row.amountSale + `</td>
	  			<td class="t-right">` + row.cpc + `</td>
	  			<td class="t-right">` + row.acos + `</td>
	  		</tr>`;
			}
			$('#id_top_product_advert_table_by_advert_desc').attr('border','1');
			$('#id_top_product_advert_table_by_advert_desc').html(h);
		}else {

		}


		if(obj) {
			$(obj).siblings().removeClass('btn-selected')
			$(obj).addClass('btn-selected');
		}
	});
}



//加载业绩趋势图
function loadYJQS(type, obj) {
	var conditions = getConditions();
	conditions['type'] = type;
	$('#idYJQSView').html(loadIngHtmlTable);

	$.post('/web/bi/yjqs', conditions, function(res){
		$('#idYJQSView').html('<div id="idYJQS" style="width: 100%;height:300px;"></div>');
		initEchartsYJQS('idYJQS', res.data);

		if(obj) {
			$(obj).siblings().removeClass('btn-selected')
			$(obj).addClass('btn-selected');
		}
	});
}

//加载库存状态
function loadKCZT() {
	$('#id_kczt_table').html(loadIngHtmlTable);
	$('#id_kczt_table').attr('border','0');
	$('#idKCZTView').html(loadIngHtmlTable);

	$.post('/web/bi/kczt', getConditions(), function(res){
		console.log('initEchartsKCZT---', res.data);
		$('#idKCZTView').html('<div id="idKCZT" style="width: 100%;height:300px;"></div>');
		initEchartsKCZT('idKCZT', res.data);
		
		// 加载右边表格
		var h = `<tr height="45">
			<th>项目</th>
			<th>库存数量</th>
			<th>库存倍数</th>
			<th>库存状况</th>
		</tr>`;
		for (var i = 0; i < res.data.rows.length; i++) {
			var row = res.data.rows[i];
			h += `<tr height="40" class="font-before">
				<td style="font-size:13px;">` + row.name + `</td>
				<td style="font-size:13px;">` + row.numStock + `</td>
				<td style="font-size:13px;">` + row.numTimes + `</td>
				<td style="font-size:13px;">` + row.condition + `</td>
			</tr>`;
		}
		$('#id_kczt_table').attr('border','1');
		$('#id_kczt_table').html(h);
	});
}

//加载广告指标对比
function loadGGZBDB() {
	$('#id_ggzbdb_table').html(loadIngHtmlTable);
	$.post('/web/bi/ggzbdb', getConditions(), function(res){
		
		// 加载右边表格
		var h = `<tr style="text-align: center;">
			<td width="30%;"></td>
			<td style="text-align: center;border-right: 1px dashed #000000;">近七天</td>
			<td style="text-align: center;">上七天</td>
		</tr>`;

		for (var i = 0; i < res.data.rows.length; i++) {
			var row = res.data.rows[i];
			h += `<tr>
				<td style="text-align: center;">` + row.name + `</td>
				<td style="border-right: 1px dashed #000000;"><div class="progress-bar-left" style="width: ` + row.percentLatest + `%; background-color: ` + (row.state == -1 ? `#ea707e` : `#51c7d7`) + `;">` + row.valueLatest + `</div></td>
				<td><div class="progress-bar-right" style="width: ` + row.percentBefore + `%;background-color: ` + (row.state == -1 ? `#51c7d7` : `#ea707e`) + `;">` + row.valueBefore + `</div></td>
			</tr>`;
		}
		$('#id_ggzbdb_table').html(h);
	});
}



//加载广告沙漏
function loadGGSL() {
	$('#idGGSLView').html(loadIngHtmlTable);
	$.post('/web/bi/ggsl', getConditions(), function(res){
		$('#idGGSLView').html('<div id="idGGSL" style="width: 100%;height:300px;"></div>');
		initEchartsGGSL('idGGSL', res.data);
	});
}

// 加载所有
function loadAll(){
	loadDPXX(0,null);
	loadDPGG(0,null);
	loadTopProduct('id_top_normal_table', 1, 1);
	loadTopProduct('id_top_jingpin_table', 2, 1);
	loadTopShop(1);
	loadTopProductAdvert(1,null,0); // 加载精品广告
	loadTopProductAdvert(1,null,1); // 加载精品广告
	loadYJQS(1); // 初始加载销售业绩图
	loadKCZT(); // 初始加载库存状态
	loadGGZBDB(); //加载广告指标对比
	loadGGSL(); //加载广告沙漏
}
loadAll();